
import java.awt.*;
import java.io.*;
import java.util.*;
import java.util.Hashtable;
import javax.swing.*;

public class test3 {
   // @SuppressWarnings("null")
    public static String fillString(char filler, int count){
        char[] Cary = new char[count];
        while(count>0){ count--;  Cary[count] = filler ;   }
        return new String(Cary);
        }
	@SuppressWarnings("null")
	private JScrollPane getContent() {
        Vector<String> vector = new Vector<String>();
        String[][] Final = null;
        Scanner s = null;
        int m = 0;
        String[] jumpaddr = null;
        String[] label = null ;
        Vector<String> vectornew = new Vector<String>();
     // Forming the Key Value pairs by using a Hasp-map, we can also use a Hash-table here!!      
        Hashtable<String, String> ht = new Hashtable<String,String>();
       
    // forming the Key value pairs for the Instructions both R-type and I-type
   
    ht.put("add","100000");
    ht.put("sub","100010");
    ht.put("load","100011");
    ht.put("Store","100100");
    ht.put("bez","100110");
    ht.put("addi","100111");
    // forming the key value pairs for the registers
   
    ht.put("r0", "00000");
    ht.put("r1", "00001");
    ht.put("r2", "00010");
    ht.put("r3", "00011");
    ht.put("r4", "00100");
    ht.put("r5", "00101");
    ht.put("r6", "00110");
    ht.put("r7", "00111");
    ht.put("r8", "01000");
    ht.put("r9", "01001");
    ht.put("r10", "01010");
    ht.put("r11", "01011");
    ht.put("r12", "01100");
    ht.put("r13", "01101");
    ht.put("r14", "01110");
    ht.put("r15", "01111");
    ht.put("r16", "10000");
    ht.put("r17", "10001");
    ht.put("r18", "10010");
    ht.put("r19", "10011");
    ht.put("r20", "10100");
    ht.put("r21", "10101");
    ht.put("r22", "10110");
    ht.put("r23", "10111");
    ht.put("r24", "11000");
    ht.put("r25", "11001");
    ht.put("r26", "11010");
    ht.put("r27", "11011");
    ht.put("r28", "11100");
    ht.put("r29", "11101");
    ht.put("r30", "11110");
    ht.put("r31", "11111");
        JTextArea textArea = new JTextArea();
        File file = new File("C:\\Users\\User\\Desktop\\CA\\example.txt");
        Scanner scanner = null;
        try {
            scanner = new Scanner(file);
        } catch(FileNotFoundException e) {
            System.out.println("file not found: " + e.getMessage());
        }
        while(scanner.hasNextLine()) {
            String line = scanner.nextLine();
            vector.addElement(line);
        }
        System.out.println(vector.size());
        scanner.close();
        for(int j = 0; j < vector.size(); j++)
            {//textArea.append(vector.get(j) + "\n");
            String temp = vector.get(j).replace(',', ' ');
            vectornew.addElement(temp);}
            //Final = 
            //System.out.println(temp);}
        for(int j = 0; j < vectornew.size(); j++)
        { textArea.append(vectornew.get(j) + "\n");}
        //String temp = vector.get(j).replace(',', ' ');
        //vectornew.addElement(Tempt);}
        for(int n=0; n<=vectornew.size(); n++)
        {   s = new Scanner(vectornew.get(n));
            //System.out.println(s);
            int j=1;
             while(s.hasNext())
              {
              Final[n][j] = s.next();
              j++;                    
              }
        }
        for (int n=0; n<=vectornew.size(); n++)
        { int j=1;
        String bez = "bez" ;
          if(Final[n][1].equals(bez))
           {
          
             m++;
            while(Final[n][j]!= null)
              { j++; }
            label[m] = Final[n][j-1];
            //System.out.println(m);
            }
         }
          
          
        
            
       for (int k=1; k<=m; k++)    
        {
         for (int n=0; n<=vectornew.size(); n++)
         	
          {int j=1; String lab = label[k] + ":" ;
          
           while(Final[n][j]!= null)
             {
                    
               if(Final[n][j].equals(lab))
              
                 {  
                  
                   String temp = (Integer.toBinaryString(n)) ;
                  
                    jumpaddr[k] = fillString('0',8-temp.length()) + temp;
                    //System.out.println(jumpaddr[k]);
                    
                   Final[n][j]= " ";  
                 }
                 j++;
           }      
          }  
        }
        
       for (int p=1; p<=m; p++)
              
         {  int k=0;
            for (int n=0; n<=vectornew.size(); n++)
            { int j=1;
            String bez = "bez" ;
               if(Final[n][1].equals(bez))
                { k++;
                 while(Final[n][j]!= null)
                  { j++; }
                   Final[n][j-1] = jumpaddr[k];
                   ht.put(Final[n][j-1],Final[n][j-1]);
                   System.out.println(Final[n][j-1]) ;
                 }
                
            }
         }
        
      
       for(int n=0; n<=vectornew.size(); n++)
         { int j=1;
            String load = "load" ;
            if(Final[n][1].equals(load))            
             {  while(Final[n][j]!=null)      
                   { j++; }    
              
               for(int r=0; r<Final[n][j-1].length();r++ )
               {  
                 if(Final[n][j-1].charAt(r)=='(')
                  {  String no = Final[n][j-1].substring(0,r);
                     //System.out.println("the value of no"+ no);
                     String reg = Final[n][j-1].substring(r+1,Final[n][j-1].length()-1) ;
                     Final[n][j-1] = reg ;
                     //System.out.println("the value of reg"+ reg);
                       int temp = Integer.parseInt(no); 
                       //System.out.println("the value of temp"+ temp);
                     String offset = Integer.toBinaryString(temp);
                     //System.out.println("the value of offset"+ offset);
                      offset = fillString('0',16-offset.length()) + offset ;
                     Final[n][j] = offset;
                     ht.put(Final[n][j],Final[n][j]);
                   }
                }  
              }
           }  
       
       for(int n=0; n<=vectornew.size(); n++)
       { int j=1;
          String store = "store" ;
          if(Final[n][1].equals(store))            
           {  while(Final[n][j]!=null)      
                 { j++; }    
            
             for(int r=0; r<Final[n][j-1].length();r++ )
             {  
               if(Final[n][j-1].charAt(r)=='(')
                {  String no = Final[n][j-1].substring(0,r);
                   //System.out.println("the value of no"+ no);
                   String reg = Final[n][j-1].substring(r+1,Final[n][j-1].length()-1) ;
                   Final[n][j-1] = reg ;
                   //System.out.println("the value of reg"+ reg);
                     int temp = Integer.parseInt(no); 
                     //System.out.println("the value of temp"+ temp);
                   String offset = Integer.toBinaryString(temp);
                   //System.out.println("the value of offset"+ offset);
                    offset = fillString('0',16-offset.length()) + offset ;
                   Final[n][j] = offset;
                   ht.put(Final[n][j],Final[n][j]);
                 }
              }  
            }
         }
        for(int n=0; n<=vectornew.size(); n++)
        {   int q = 1; 
     	   String addi = "addi" ;
        if(Final[n][1].equals(addi))
        { while(Final[n][q]!=null)      
             { q++; } 
        String noimm = Final[n][q-1];
        int temp = Integer.parseInt(noimm);
        String immvalue = Integer.toBinaryString(temp);
        System.out.println("the value of immidiate value"+ immvalue);   
        immvalue = fillString('0',16-immvalue.length()) + immvalue ;
        Final[n][q] = immvalue;
        ht.put(Final[n][q],Final[n][q]);
        }
        
        }     
         for(int n=0; n<=vectornew.size(); n++)
         { int j=1;
           while(Final[n][j]!=null)
            {  System.out.println("Final[" + n +"][" +j+ "]" +   ht.get(Final[n][j]));
                j++;
            }
         }
                
         for(int n=0; n<=vectornew.size(); n++)
         { int j=1;
           String line = "";
           String k ="";
           while(Final[n][j]!=null)
            {      
              if(ht.get(Final[n][j])!=null)
                k = ht.get(Final[n][j]) ;
               else
               k="";
                
           line = line + k;
                j++;
            }      
                
              
              line = line + fillString('0',32-line.length()) ;
              System.out.println(line) ;
          
            }        
                
        //Final = vectornew.toString();
        //System.out.println(temp);}
        
        return new JScrollPane(textArea);
        
    }
          
	public static void main(String[] args) {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(new test3().getContent());
        f.setSize(400,400);
        f.setLocation(200,200);
        f.setVisible(true);
    }
}